package com.zcf.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zcf.domain.User;
import com.zcf.service.UserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by 11481 on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAciton extends ActionSupport implements ModelDriven<User> {
    private String message;
    private String username;
    private String password;
    private String password2;
    private User model=new User();
    @Resource
    private UserService userService;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String registerUI(){
        return "registerUI";
    }
    public String loginUI(){
        return "loginUI";
    }
    public String login(){
        boolean b = userService.findByUser(model.getUsername(),model.getPassword());

        if (b){
            message = "登录成功！";
            List<User> userList=userService.findByList();
            ActionContext.getContext().put("userList",userList);
            return "loginSuccess";
        }else {
            message = "登录失败！";
            return "loginFail";
        }
    }
    public String register(){
        boolean b = userService.findByName(username);
        if (b){
            message = "帐号已存在，注册失败！";
            return "fail";
        }else {
            if (password.equals(password2)){
                userService.register(model.getUsername(),model.getPassword());
                message = "注册成功！";
                return "success";
            }else {
                message = "密码不一致！";
                return "fail";
            }
        }
    }

    @Override
    public User getModel() {
        return model;
    }
}
